<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../style/js/jquery-1.8.3.min.js"></script>
    <script src="../style/js/ajaxfileupload.js"></script>
    <script>

        function checkPassword() {
            var id = window.sessionStorage.getItem("id");
            var oldPassword = $("#oldPassword").val();
            $.get("../userInterface/selectPassword", {password: oldPassword, id: id}, function (res) {
                if (res.code == 200) {
                    $("#spanId1").html("<b>√</b>");
                } else if (res.code == 300) {
                    $("#spanId1").html("<b>请输入正确密码</b>");
                } else {
                    $("#spanId1").html("<b>!!!</b>")
                }
            })
        }

        function checkNewPassword() {
            var oldPassword = $("input[name='OldPassword']").val();
            var newPassword1 = $("input[name='NewPassword1']").val();
            var newPassword2 = $("input[name='NewPassword2']").val();

            if ((newPassword1.length != null || newPassword1 != null) || (newPassword2.length != null || newPassword2 != null)) {
                if (newPassword1 != newPassword2) {
                    $("#spanId3").html("<b>两次输入的密码不一致</b>");
                } else if (newPassword1 == '' || newPassword2 == '') {
                    $("#spanId3").html("<b>新密码不能为空,请重新输入!</b>");
                } else if (newPassword1 == oldPassword) {
                    $("#spanId2").html("<b>新密码不能与原密码相同!</b>");
                } else {
                    $("#spanId3").html("<b>√</b>")
                    $("#spanId2").html("");
                }
            }
        }

        function updateNewPassword() {

            var id = window.sessionStorage.getItem("id");
            var oldPassword = $("input[name='oldPassword']").val();
            var newPassword1 = $("input[name='NewPassword1']").val();
            var newPassword2 = $("input[name='NewPassword2']").val();


            if (newPassword1 == newPassword2 && oldPassword != newPassword1) {
                if (newPassword1.length != 0 || newPassword1 != null) {
                    if (newPassword1 != '') {
                        $.get("../userInterface/saveNewPassword", {password: newPassword1, id: id}, function (res) {
                            if (res.code == 200) {
                                alert("修改成功,请重新登陆");
                                parent.location.replace("../user/userLogin.html");
                            } else {
                                alert("修改失败");
                            }
                        })
                    } else {
                        alert("请输入内容后再进行操作!");
                    }
                }
            } else {
                alert("请正确输入!");
            }
        }
    </script>
</head>
<body>
<div>
    <div class="changePassword">
        <form id="formId">
            <p>
                <em>原密码：</em><input type="password" name="OldPassword" id="oldPassword" onblur="checkPassword()">
            </p>
            <span id="spanId1"></span>
            <p>
                <em>新密码：</em><input type="password" name="NewPassword1" onblur="checkNewPassword()">
            </p>
            <span id="spanId2"></span>
            <p>
                <em>再次输入：</em><input type="password" name="NewPassword2" onblur="checkNewPassword()">
            </p>
            <span id="spanId3"></span>
        </form>
        <input type="button" onclick="updateNewPassword()" value="保存修改">
    </div>
</div>
</body>
</html>